<?php
/* Immanuel192 Fix this module on 8/12/2009 */
/**
* Change logs
* Sep 12, 2011 - trungdt - Add agregate to Article module
* 
*/
class articlemodule {
	function name() { return "Article Module"; }
	function description() { return "Manages Articles"; }
	function author() { return "Immanuel192"; }
	
	function hasSources() { return true; }
	function hasContent() { return true; }
	function hasViews() { return true; }
	
	function supportsWorkflow() { return false; }
	
	function permissions($internal = "") {
		return array(
			'administrate'=>'Administrate',
			'configure'=>'Configure',
			'manage'=>'Manage Articles',
			'create'=>'Create Articles',
		);
	}
		
	function show($view,$loc = null, $title = "") {
		// set page to 0
		$_REQUEST['page']=1;
		$_REQUEST['flow_has_set']=1; // to compatipility with view_page
		exponent_flow_set(SYS_FLOW_PUBLIC,SYS_FLOW_SECTIONAL);
		include("actions/view_page.php");
	}
	
	function deleteIn($loc) {
		global $db;
		$db->delete('article',"location_data='".serialize($loc)."'");
		$db->delete('category', "location_data='".serialize($loc)."'");
		$db->delete('articlemodule_config', "location_data='".serialize($loc)."'");
	}
	
	function copyContent($oloc,$nloc) {
		global $db;
		$catid_map = array();
		
		foreach ($db->selectObjects('category',"location_data='".serialize($oloc)."'") as $category) {
			$oldid = $category->id;
			unset($category->id);
			$category->location_data = serialize($nloc);
			$catid_map[$oldid] = $db->insertObject($category,'category');
		}
		
		foreach ($db->selectObjects('article',"location_data='".serialize($oloc)."'") as $object) {
			unset($object->id);
			$object->location_data = serialize($nloc);
			if (isset($catid_map[$object->category_id])) {
				$object->category_id = $catid_map[$object->category_id];
			} else {
				$object->category_id = 0;
			}
			$db->insertObject($object,'article');
		}
		
		
		$conf = $db->selectObject('articlemodule_config', "location_data='".serialize($oloc)."'");
		unset($conf->id);
		$conf->location_data = serialize($nloc);
		$db->insertObject($conf,'articlemodule_config');
	}

	function searchName() {
		$lang=exponent_lang_loadFile("modules/articlemodule/class.php");
			return $lang['search_name'];
	}
	
	function spiderContent($item = null) {
		global $db;
		
		if (!defined('SYS_SEARCH')) require_once(BASE.'subsystems/search.php');
		
		$search = null;
		$search->category = 'Articles';
		$search->ref_module = 'articlemodule';
		$search->ref_type = 'article';
		
		if ($item) {
			$db->delete('search',"ref_module='articlemodule' AND ref_type='article' AND original_id=" . $item->id);
			$search->original_id = $item->id;
			$search->title = ' ' . $item->title . ' ';
			$search->view_link = 'index.php?module=articlemodule&ajax_action=1&action=view_article&id='.$item->id;
			$search->body = ' ' . exponent_search_removeHTML($item->body) . ' ';
			$search->location_data = $item->location_data;
			$db->insertObject($search,'search');
		} else {
			$db->delete('search',"ref_module='articlemodule' AND ref_type='article'");
			foreach ($db->selectObjects('article') as $item) {
				$search->original_id = $item->id;
				$search->title = ' ' . $item->title . ' ';
				$search->view_link = 'index.php?module=articlemodule&ajax_action=1&action=view_article&id='.$item->id;
				$search->body = ' ' . exponent_search_removeHTML($item->body) . ' ';
				$search->location_data = $item->location_data;
				$db->insertObject($search,'search');
			}
		}
		
		return true;
	}
}

?>
